<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="zh">
<head>
    <meta charset="UTF-8">
    <title>宿舍管理</title>
    <script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-gray-100 min-h-screen">

<!-- 顶栏 -->
<header class="bg-white shadow px-6 py-4 flex justify-between items-center">
    <div class="text-xl font-bold text-gray-700">宿舍管理系统</div>
    <div class="flex items-center space-x-4">
        <input type="text" placeholder="搜索宿舍..."
               class="border rounded px-3 py-1 text-sm focus:outline-none focus:ring"/>
        <span class="text-gray-600" th:text="${session.get('loginUsername')}">admin</span>
        <a href="/doLogout" class="text-blue-500 hover:underline" th:if="${session.get('loginUsername')}">退出登录</a>
        <a href="/login" class="text-blue-500 hover:underline" th:if="${session.get('loginUsername')==null}">去登录</a>
        <a href="/login?admin=true" class="text-blue-500 hover:underline" th:if="${session.get('loginUsername')==null}">管理员登录</a>
        <a href="/register" class="text-blue-500 hover:underline" th:if="${session.get('loginUsername')==null}">学生注册</a>
    </div>
</header>

<!-- 内容区域 -->
<main class="p-6">
    <div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-6">
        <div th:each="dorm : ${dormitoryPage.getRecords()}"
             class="group relative rounded-lg overflow-hidden shadow-md bg-white transition-transform transform hover:scale-105">

            <!-- 正面内容 -->
            <div class="p-4 transition-opacity duration-300 group-hover:opacity-0">
                <!-- 图片 -->
                <img th:src="${dorm.image}" alt="宿舍照片" class="w-full h-40 object-cover rounded-md mb-4">

                <!-- 编号 + 类型徽章 -->
                <div class="flex items-center justify-between mb-3">
                    <div class="font-semibold text-lg" th:text="${'ID:'+dorm.dormId+' 编号:'+dorm.building+'-'+dorm.dormNumber}">A101</div>
                    <div th:classappend="${dorm.dormType == 0} ? 'bg-blue-100 text-blue-800' : 'bg-pink-100 text-pink-800'"
                         class="text-xs px-2 py-1 rounded">
                        <span th:text="${dorm.dormType == 0 ? '男生宿舍' : '女生宿舍'}">宿舍类型</span>
                    </div>
                </div>

                <!-- 两列信息集中展示 -->
                <div class="grid grid-cols-2 gap-x-4 text-sm text-gray-600">
                    <div>总床位：<span th:text="${dorm.totalBeds}">6</span></div>
                    <div>已入住：<span th:text="${dorm.occupiedBeds}">4</span></div>
                    <div>剩余床位：<span th:text="${dorm.availableBeds}">2</span></div>
                    <div>楼栋：<span th:text="${dorm.building}">1栋</span></div>
                    <div>楼层：<span th:text="${dorm.floor}">3层</span></div>
                    <div>舍长：<span th:text="${dorm.manager}">李雷</span></div>
                </div>
            </div>

            <!-- 悬浮展示床位图 -->
            <div class="absolute inset-0 bg-white p-4 opacity-0 group-hover:opacity-100 transition-opacity duration-300">
                <div class="text-sm font-medium mb-2">床位分布：</div>

                <!-- 动态行计算 -->
                <div th:with="rows=${T(java.lang.Math).ceil(dorm.totalBeds / 4.0)}">
                    <div th:each="row : ${#numbers.sequence(1, rows)}" class="flex gap-2 mb-2">

                        <!-- 每行最多4个床位 -->
                        <div th:each="i : ${#numbers.sequence(0, 3)}"
                             th:with="idx=${(row - 1) * 4 + i}"
                             th:if="${idx < dorm.totalBeds}"
                             th:class="${idx < dorm.students.size()} ?
               'flex-1 h-10 flex items-center justify-center border rounded text-xs bg-green-100 text-green-800' :
               'flex-1 h-10 flex items-center justify-center border rounded text-xs bg-gray-100 text-gray-500'">

                            <!-- 正确获取学生姓名 -->
                            <span th:text="${idx < dorm.students.size() ? dorm.students.get(idx).name : '空床'}"></span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 分页 -->
    <div class="mt-6 flex justify-center space-x-2 text-sm text-gray-700">
        <a th:each="i : ${#numbers.sequence(1, dormitoryPage.getPages())}"
           th:href="@{'/dormitory?page=' + ${i}}"
           th:classappend="${i == dormitoryPage.getCurrent()} ? 'bg-blue-500 text-white' : 'bg-white hover:bg-gray-100'"
           class="px-3 py-1 rounded border shadow-sm">
            <span th:text="${i}">1</span>
        </a>
    </div>
</main>

</body>
</html>
